﻿@model ShoppingCartModel.DiscountBoxModel

@if (Model.Display)
{
    <div class="coupon-box">
        <h2 class="h5">
            <strong>
                @T("ShoppingCart.DiscountCouponCode.Tooltip")
            </strong>
        </h2>
        <div class="coupon-code">
            <div class="form-group">
                <span class="col-form-label">@T("ShoppingCart.DiscountCouponCode")</span>
                <div class="input-group">
                    <input type="text" id="discountcouponcode" name="discountcouponcode" class="form-control discount-coupon-code">
                    <label for="discountcouponcode" class="sr-only">@T("ShoppingCart.GiftCardCouponCode.Button")</label>
                    <span class="input-group-append">
                        <button type="button" name="applydiscountcouponcode" id="applydiscountcouponcode" class="btn btn-outline-secondary apply-discount-coupon-code-button"
                                data-href="@Url.RouteUrl("ApplyDiscountCoupon")">
                            @T("ShoppingCart.DiscountCouponCode.Button")
                        </button>
                    </span>
                </div>
            </div>
        </div>
        <script asp-location="Footer">
            $(document).ready(function () {
                $('#discountcouponcode').keydown(function (event) {
                    if (event.keyCode == 13) {
                        $('#applydiscountcouponcode').trigger("click")
                        return false;
                    }
                });
            });
        </script>

        @if (!String.IsNullOrEmpty(Model.Message))
        {
            <div class="card border-0 text-white mb-3 @(Model.IsApplied ? "card-success message-success" : "card-danger message-failure") text-center">
                <div class="card-body p-3">
                    <blockquote class="card-bodyquote mb-0">
                        <span>@Model.Message</span>
                    </blockquote>
                </div>
            </div>
        }
        @foreach (var discount in Model.AppliedDiscountsWithCodes)
        {

            <div class="card border-0 text-white card-success text-center current-code mb-3">
                <div class="card-body d-inline-flex align-items-center p-3">
                        <span>@string.Format(T("ShoppingCart.DiscountCouponCode.CurrentCode").Text, discount.CouponCode)</span>
                        <button type="button" name="removediscount"
                                data-href="@Url.RouteUrl("RemoveDiscountCoupon", new { discountId = discount.Id})"
                                title="@T("Common.Remove")" class="border-0 bg-transparent text-white ml-auto btn btn-sm btn-secondary remove-discount-button float-sm-right">
                            <span class="mdi mdi-close" aria-hidden="true"></span>
                        </button>
                </div>
            </div>
        }
    </div>
    <script asp-location="Footer">
        $(document).ready(function () {
            $("#applydiscountcouponcode").click(function () {
                var href = $(this).closest('[data-href]').data('href');
                var couponCodeData = {
                    discountcouponcode: $('#discountcouponcode').val(),
                };
                addAntiForgeryToken(couponCodeData);
                $.ajax({
                    cache: false,
                    type: "POST",
                    data: couponCodeData,
                    url: href,
                    success: function (data) {
                        $('#ordersummarypagecart').replaceWith(data.cart);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve Shopping Cart Page.');
                    }
                });
            });
            $(".remove-discount-button").click(function () {
                var href = $(this).closest('[data-href]').data('href');
                $.ajax({
                    cache: false,
                    type: "POST",
                    data: addAntiForgeryToken(),
                    url: href,
                    success: function (data) {
                        $('#ordersummarypagecart').replaceWith(data.cart);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve Shopping Cart Page.');
                    }
                });
            });
        });
    </script>

}
